<template>
  <el-drawer :title="title" :before-close="handleClose" :visible.sync="addShow" direction="rtl" custom-class="demo-drawer" size="840px">
    <div class="demo-drawer__content">
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="名称" prop="title">
          <el-input v-model="form.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <!-- <upload-file :limit="1" accept="image/*" btnSize="small" @change="fileChange" listType='picture-card' :imgFlag='true'></upload-file> -->
          <upload-image :limit="1" :file-list-user="fileList" accept="image/*" btn-size="small" list-type="picture-card" :img-flag="true" @change="fileChange" />
        </el-form-item>
        <el-form-item label="开课时间" prop="beginAt">
          <el-date-picker v-model="form.beginAt" type="datetime" placeholder="选择日期时间" format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" />
        </el-form-item>
        <el-form-item label="简介" prop="content">
          <el-input v-model="form.content" type="textarea" :rows="3" placeholder="请输入简介" />
        </el-form-item>
        <!-- <el-form-item label="描述">
          <el-input v-model="form.content" autocomplete="off"></el-input>
        </el-form-item> -->
        <el-form-item>
          <el-button class="gradientVertical" type="primary" @click="submitForm('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>

<script>
import uploadImage from '../../../components/upload-image'
export default {
  components: { uploadImage },
  props: {
  },
  data () {
    return {
      addShow: false,
      direction: 'rtl',
      title: '添加直播',
      form: {
        title: '',
        cover: '',
        beginAt: '',
        content: '',
        id: null
      },
      rules: {
        title: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        cover: [
          { required: true, message: '请选择封面', trigger: 'blur' }
        ],
        beginAt: [
          { required: true, message: '请选择开课时间', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入简介', trigger: 'blur' }
        ]
      },
      fileList: [],
      type: ''
    }
  },
  methods: {
    mainEditFun (type, obj) {
      this.type = type
      this.addShow = true
      this.title = type === 'add' ? '添加直播' : '编辑直播'
      this.form = {
        title: obj.title,
        cover: obj.cover,
        beginAt: obj.beginAt,
        content: obj.content,
        id: obj.id
      }
      if (obj.cover && obj.cover !== '') {
        this.fileList = [{ url: this.form.cover }]
      } else {
        this.fileList = []
      }
    },
    // 获取上传的图片
    fileChange (files) {
      this.form.cover = files[0].response.data.url
    },
    // 确定
    submitForm (formName) {
      console.log(this.form)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addShow = false
          this.$emit('addFun', this.type, this.form)
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    handleClose (done) {
      this.$refs['ruleForm'].resetFields()
      this.form = {
        title: '',
        cover: '',
        beginAt: '',
        content: '',
        id: null
      }
      this.fileList = []
      console.log(this.form)
      this.addShow = false
    },
    getLocalTime (nS) {
      return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ')
    }
  }
}
</script>

<style lang='scss'>
</style>
